package com.bocai.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * Created by VIC on 2017/2/19.
 */
public class ResponseUtils {
    public static void writeJson(Object obj, HttpServletResponse response) throws IOException {
        String s = JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter printwriter = response.getWriter();
        printwriter.print(s);
        printwriter.close();
    }

    public static void writeResponse(Object result, boolean success, HttpServletResponse response) throws IOException {
        Map<String, Object> messageMap = new HashMap<String, Object>();
        messageMap.put("success", success);
        messageMap.put("result", result);
        writeJson(messageMap, response);
    }
}
